package my.service;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.widget.Toast;

public class MainActivity extends Activity {
	private myService s;
	private Intent intentServ;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        /* This is the important part */
        doBindService();
    }

    @Override
    protected void onDestroy() {
    	super.onDestroy();
    	doUnBindService();
    }
    
	private ServiceConnection mConnection = new ServiceConnection() {

		public void onServiceConnected(ComponentName className, IBinder binder) {
			s = ((myService.MyBinder) binder).getService();
			Toast.makeText(MainActivity.this, "Connected",
					Toast.LENGTH_SHORT).show();
		}

		public void onServiceDisconnected(ComponentName className) {
			s = null;
		}
	};

	void doBindService() {
		intentServ = new Intent(this, myService.class);
		bindService(intentServ , mConnection,	Context.BIND_AUTO_CREATE);
	}
	
	void doUnBindService() {
		unbindService(mConnection);
	}
}